"use strict";
const path = require("path");
const { title, abbreviation, devPort } = require("./src/config/settings");
const pkg = require("./package.json");
const Webpack = require("webpack");
const WebpackBar = require("webpackbar");
const FileManagerPlugin = require("filemanager-webpack-plugin");

const date = new Date(),
  year = date.getFullYear(),
  month = date.getMonth() + 1,
  day = date.getDate(),
  hours = date.getHours(),
  minutes = date.getMinutes(),
  seconds = date.getSeconds(),
  time = `"${year}-${month}-${day} ${hours}:${minutes}:${seconds}"`;

function resolve(dir) {
  return path.join(__dirname, dir);
}

// function mockServer() {
//   if (process.env.NODE_ENV === "development") {
//     const mockServer = require("./mock/mock-server.js");
//     return mockServer;
//   } else {
//     return "";
//   }
// }

const name = title || "海豚淘客后台管理";

module.exports = {
  lintOnSave: false,
  publicPath: "/",
  assetsDir: "static",
  outputDir: "dist",
  transpileDependencies: ["vue-echarts", "resize-detector"],

  devServer: {
    hot: true,
    compress: true,
    port: devPort,
    open: true,
    noInfo: false,
    overlay: {
      warnings: true,
      errors: true,
    },
    // proxy: {
    //   "/api": {
    //     /* 目标代理服务器地址 */
    //     target: `http://192.168.1.141:8001?s=`, //后端电脑
    //     /* 允许跨域 */
    //     changeOrigin: true,
    //     pathRewrite: {},
    //   },
    // },
    // after: mockServer(),
  },

  configureWebpack(config) {
    return {
      name: name,
      resolve: {
        alias: {
          "@": resolve("src"),
          "^": resolve("src/components"),
        },
      },
      plugins: [
        new Webpack.ProvidePlugin({
          $: "jquery",
          jQuery: "jquery",
          "windows.jQuery": "jquery",
          echarts: "echarts",
          "window.echarts": "echarts",
          maptalks: "maptalks",
          "window.maptalks": "maptalks",
        }),
        new Webpack.DefinePlugin({
          "process.env.VUE_APP_UPDATE_TIME": time,
        }),
        new WebpackBar({
          name: `by kudzu ${pkg.name}`,
        }),
      ],
    };
  },

  chainWebpack(config) {
    config.resolve.symlinks(true);
    config.module
      .rule("svg")
      .exclude.add(resolve("src/icons"))
      .add(resolve("src/remixicon"))
      .end();
    config.module
      .rule("icons")
      .test(/\.svg$/)
      .include.add(resolve("src/icons"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({ symbolId: "icon-[name]" })
      .end();
    config.module
      .rule("remixicon")
      .test(/\.svg$/)
      .include.add(resolve("src/remixicon"))
      .end()
      .use("svg-sprite-loader")
      .loader("svg-sprite-loader")
      .options({ symbolId: "remixicon-[name]" })
      .end();

    config.module
      .rule("vue")
      .use("vue-loader")
      .loader("vue-loader")
      .tap((options) => {
        options.compilerOptions.preserveWhitespace = true;
        return options;
      })
      .end();
    config.when(process.env.NODE_ENV === "development", (config) => {
      config.devtool("cheap-module-eval-source-map");
    });
    config.when(process.env.NODE_ENV !== "development", (config) => {
      config.performance.set("hints", false);
      config.devtool("none");
      config
        .plugin("ScriptExtHtmlWebpackPlugin")
        .after("html")
        .use("script-ext-html-Webpack-plugin", [{ inline: /runtime\..*\.js$/ }])
        .end();
      config.optimization.splitChunks({
        chunks: "all",
        cacheGroups: {
          libs: {
            name: "chunk-libs",
            test: /[\\/]node_modules[\\/]/,
            priority: 10,
            chunks: "initial",
          },
          elementUI: {
            name: "chunk-elementUI",
            priority: 20,
            test: /[\\/]node_modules[\\/]_?element-ui(.*)/,
          },
          wantUI: {
            name: "chunk-wantUI",
            priority: 10,
            test: /[\\/]node_modules[\\/]_?want(.*)/,
          },
          commons: {
            name: "chunk-commons",
            test: resolve("src/components"),
            minChunks: 3,
            priority: 5,
            reuseExistingChunk: true,
          },
        },
      });
      config.optimization.runtimeChunk("single");
      config
        .plugin("banner")
        .use(Webpack.BannerPlugin, [
          `淘客工具 \n QQ:3558331 \n author:Kudzu \n 发布时间: ${time}`,
        ])
        .end();
    });
    config.when(process.env.NODE_ENV === "production", (config) => {
      config
        .plugin("fileManager")
        .use(FileManagerPlugin, [
          {
            onEnd: {
              delete: ["./dist/video", "./dist/data"],
              archive: [
                {
                  source: "./dist",
                  destination: `./dist/${abbreviation}_dist_${year}.${month}.${day}.7z`,
                },
              ],
            },
          },
        ])
        .end();
    });
  },

  runtimeCompiler: true,
  productionSourceMap: false,

  css: {
    requireModuleExtension: true,
    sourceMap: true,
    loaderOptions: {
      scss: {
        prependData: '@import "~@/styles/variables.scss";',
      },
    },
  },
};
